package org.cocos2dx.javascript;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.byteedu.yyb.landlord.R;

import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;
import org.json.JSONException;
import org.json.JSONObject;


public final class ToolHelp {
    private static String TAG = "ToolHelp";
    private static ToolHelp sInstance = null;
    private static ImageView sSplashBgImageView = null;
    private static AppActivity sApp = null;

    public static ToolHelp getInstance(){
        if (sInstance == null){
            sInstance = new ToolHelp();
        }
        return sInstance;
    }

    public void init(AppActivity app){
        sApp = app;
        // 保持屏幕常亮
        app.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        // 添加启动图
        sSplashBgImageView = new ImageView(app);
        sSplashBgImageView.setImageResource(R.drawable.launch);
        sSplashBgImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        app.addContentView(sSplashBgImageView,
            new WindowManager.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
        );

        RewardVideo.getInstance().init(app);
    }

    public static void callNative(final String flag, final String JSON){
        if (flag == null || flag.isEmpty()){
            Log.e(TAG,String.format("callNative(flag == null || flag.isEmpty())"));
            return;
        }
        Log.d(TAG,String.format("callNative:%s,%s",flag,JSON));
        if (flag.equals("closeLaunch")){
            sInstance.closeLaunchSplish();
        }
        if (flag != null || !flag.isEmpty()){
            try {
                if (flag.equals("playRewardVideo")){
                    JSONObject res = new JSONObject(JSON);
                    if (res != null){
                        RewardVideo.getInstance().loadAd(res.getString("ID"),res.getString("openID"));
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 回调JS层
     * @param flag 回调JS层事件
     * @param data 数据
     */
    public void callJS(final String flag, final String data){
        sApp.runOnGLThread(new Runnable() {
            @Override
            public void run() {
                String evalString =  String.format("cc.director.emit('NativeMessageCall','%s',%s);",flag,data);
                Cocos2dxJavascriptJavaBridge.evalString(evalString);
                Log.d(TAG,evalString);
            }
        });
    }

    // 关闭启动图
    private void closeLaunchSplish() {
        sApp.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (sSplashBgImageView != null) {
                    sSplashBgImageView.animate().alpha(0f)
                            .setDuration(400).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            sSplashBgImageView.setVisibility(View.GONE);
                        }
                    });
                }
            }
        });
    }
}
